<?php

class friends
{
	public $TABLE_NAME_FRIENDS = 'Friends';
	public $TABLE_NAME_REQUESTS = 'Friends_requests';
	
	public $TABLE_COLUMNS_FRIENDS = array(
			'id'		=> 'id_User',
			'list'		=> 'Friends_list'
		);
	private $DB = null;
		
	function __construct( $DB )
	{
		if ( $DB )
		{
			$this->DB = $DB;
		}
	}
	
	function get_list_for( $User_id )
	{
		$get_list_sql = sprintf( "
			SELECT 
				*
			FROM
				`%s`
			WHERE
				`%s`='%d'
		",
			$this->TABLE_NAME_FRIENDS,
			$this->TABLE_COLUMNS_FRIENDS['id'],
			$User_id
				);
				
		$get_list_result = $this->DB->db_query( $get_list_sql );
		$friends = array();
		if ( $get_list_result['num_rows'] )
		{
			$friends = explode( '|', $get_list_result['data'][0][ $this->TABLE_COLUMNS_FRIENDS['list'] ] );
		}
		
		return $friends;
	}
	
	function add_friend_for( $User_id, $Friend_id )
	{
		if ( $User_id == $Friend_id )
		{
			return false;
		}
		
		$User_id = (int)$User_id;
		$Friend_id = (int)$Friend_id;
		
		if ( $this->is_users_friend( $User_id, $Friend_id ) )
		{
			return 'ALREADY_FRIENDS';
		}
		else
		{
			$users_friends[] = $Friend_id;
			$new_friend_list = implode( '|', $users_friends );
			$add_friend_sql = sprintf( "
				UPDATE 
					`%s`
				SET
					`%s`='%s'
				WHERE
					`%s`='%d'
				",
				$this->TABLE_NAME_FRIENDS,
				$this->TABLE_COLUMNS_FRIENDS['list'],
				$new_friend_list,
				$this->TABLE_COLUMNS_FRIENDS['id'],
				$User_id
			 );
			$add_friend_result = $this->DB->db_query( $add_friend_sql );
			
			if ( $add_friend_result['affected_rows'] )
			{
				return true;
			}
			else
			{
				$add_new_friend_sql = sprintf( "
					INSERT INTO
						`%s`
						( 
							`%s`,
							`%s`
						)
						VALUES	
						(
							'%d',
							'%d'
						)
					",
						$this->TABLE_NAME_FRIENDS,
						$this->TABLE_COLUMNS_FRIENDS['id'],
						$this->TABLE_COLUMNS_FRIENDS['list'],
						$User_id,
						$Friend_id
					 );
				$add_new_friend_result = $this->DB->db_query( $add_new_friend_sql );
				if ( $add_new_friend_result['insert_id'] )
				{
					return true;
				}
				else
				{
					return $add_new_friend_result['error'];	
				}
			}
		}
	}
	
	function is_users_friend( $User_id, $Friend_id )
	{
		$User_id = (int)$User_id;
		$Friend_id = (int)$Friend_id;	
		$users_friends = $this->get_list_for( $User_id );
		if ( in_array( $Friend_id, $users_friends ) )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function delete_friend_for( $User_id, $Friend_id )
	{
		$User_id = (int)$User_id;
		$Friend_id = (int)$Friend_id;
		
		$users_friends = $this->get_list_for( $User_id );
		
		$index = array_search( $Friend_id, $users_friends );
		
		if ( $index !== FALSE )
		{
			unset( $users_friends[$index] );
			
			$new_friend_list = implode( '|', $users_friends );
			$update_user_list_sql = sprintf( "
				UPDATE
					`%s`
				SET
					`%s`='%s'
				WHERE
					`%s`='%d'
				",
					$this->TABLE_NAME_FRIENDS,
					$this->TABLE_COLUMNS_FRIENDS['list'],
					$new_friend_list,
					$this->TABLE_COLUMNS_FRIENDS['id'],
					$User_id
				 );
				
			$update_user_list_result = $this->DB->db_query( $update_user_list_sql );
			if ( $update_user_list_result['affected_rows'] )
			{
				return true;
			}
			else
			{
				return $update_user_list_result['error'];
			}
		}
		else
		{
			return 'NOT_MY_FRIEND';
		}		
	}
}


?>